package com.example.cupid.controller;

import com.example.cupid.pojo.*;
<<<<<<< HEAD
import com.example.cupid.service.AdminService;
import com.example.cupid.service.Impl.OrderServiceImpl;
=======
>>>>>>> 9a66e53171eff92bf88f5bdf58a30715596c289f
import com.example.cupid.service.Impl.UserServiceImpl;
import com.example.cupid.service.Impl.WechatServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;
<<<<<<< HEAD

import javax.servlet.http.HttpSession;
import java.util.*;
=======
import javax.servlet.http.HttpSession;
import java.util.List;
>>>>>>> 9a66e53171eff92bf88f5bdf58a30715596c289f

@Controller
@RequestMapping("/admin")
public class AdminController {
<<<<<<< HEAD

    @Autowired
    private AdminService adminService;
=======
>>>>>>> 9a66e53171eff92bf88f5bdf58a30715596c289f
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private WechatServiceImpl wechatService;
<<<<<<< HEAD
    @Autowired
    private OrderServiceImpl orderService;
=======
>>>>>>> 9a66e53171eff92bf88f5bdf58a30715596c289f
    @GetMapping
    public String loginPage() {
        return "admin/login";
    }

    @GetMapping("/info")
    public String Info(){return "admin/info";}

    @GetMapping("/userTable")
    public String table(Model model) {
        List<User> list = userService.findAll();
        model.addAttribute("user",list);
        return "admin/table-user";
    }

    @GetMapping("/tableSave")
    public String saveTable(Model model){
        List<UserWechatSava> list = wechatService.getSaveWechat();
        model.addAttribute("userSave",list);
        return "admin/table-user-save";
    }

    @GetMapping("/tableTake")
    public String tableTake(Model model){
        List<UserWechatTake> list = wechatService.getTakeWechat();
        model.addAttribute("userTake",list);
        return "admin/table-user-take";
    }

    @GetMapping("/index")
<<<<<<< HEAD
    public String index(Model model){
        //SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date utilDate=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(utilDate);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date dateYd= calendar.getTime();
        java.sql.Date sqlDateYd = new java.sql.Date(dateYd.getTime());
        java.sql.Date sqlDate=new java.sql.Date(utilDate.getTime());
        //计算新增人数
        int counttd = adminService.userCountTd(sqlDate);
        int count = adminService.userCountYd(sqlDateYd);
        int countyd = count - counttd;
        float percent,orderPercent;
        if(countyd ==0) {
            if (counttd==0)
                 percent=0;
            else
                percent=1;
        }
        else {
            percent = (float) counttd / (float) countyd;
        }
        //计算交易额
        int countOrderTd = orderService.checkOrderTd(sqlDate);
        int countOrder = orderService.checkOrderYd(sqlDateYd);
        int countOrderYd = countOrder - countOrderTd;
        if (countOrderYd ==0){
            if (countOrderTd !=0)
                orderPercent =1;
            else
                orderPercent=0;
        }else {
            orderPercent = (float) countOrderTd / (float) countOrderYd;
        }
        if (orderPercent>1) orderPercent = 1 ;
        if (percent>1) percent = 1;
        //付款总额排行
        List<User> userList = adminService.findAllByPayDesc();
        List<User> list = new ArrayList<>();
        for (int i = 0; i < userList.size(); i++) {
            if (i<5)
                list.add(userList.get(i));
            else break;
        }
        for (User user : list) {
            System.out.println(user);
        }
        System.out.println(""+percent);
        System.out.println(""+orderPercent);
        model.addAttribute("count",counttd);
        model.addAttribute("percent",percent*100);
        model.addAttribute("countOrder",countOrderTd);
        model.addAttribute("percentOrder",orderPercent*100);
        model.addAttribute("userList",list);
        return "admin/index";
    }

    @PostMapping("/login")
    public String login(@RequestParam String email,@RequestParam String password, HttpSession session, RedirectAttributes attributes,Model model) {
        Admin admin = adminService.checkUser(email, password);
        System.out.println(admin);
        //SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date utilDate=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(utilDate);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date dateYd= calendar.getTime();
        java.sql.Date sqlDateYd = new java.sql.Date(dateYd.getTime());
        java.sql.Date sqlDate=new java.sql.Date(utilDate.getTime());
        //计算新增人数
        int counttd = adminService.userCountTd(sqlDate);
        int count = adminService.userCountYd(sqlDateYd);
        int countyd = count - counttd;
        float percent,orderPercent;
        if(countyd ==0) {
            if (counttd==0)
                percent=0;
            else
                percent=1;
        }else {
            percent = (float) counttd / (float) countyd;
        }
        //计算交易额
        int countOrderTd = orderService.checkOrderTd(sqlDate);
        int countOrder = orderService.checkOrderYd(sqlDateYd);
        int countOrderYd = countOrder - countOrderTd;
        if (countOrderYd ==0){
            if (countOrderTd !=0)
                orderPercent =1;
            else
                orderPercent=0;
        }else {
            orderPercent = (float) countOrderTd / (float) countOrderYd;
        }
        if (orderPercent>1) orderPercent = 1 ;
        if (percent>1) percent = 1;
        //付款总额排行
        List<User> userList = adminService.findAllByPayDesc();
        List<User> list = new ArrayList<>();
        for (int i = 0; i < userList.size(); i++) {
            if (i<5)
                list.add(userList.get(i));
            else break;
        }
        for (User user : list) {
            System.out.println(user);
        }
        System.out.println(""+percent);
        System.out.println(""+orderPercent);
        if (admin != null) {
            session.setAttribute("admin", admin);
            model.addAttribute("count",counttd);
            model.addAttribute("percent",percent*100);
            model.addAttribute("countOrder",countOrderTd);
            model.addAttribute("percentOrder",orderPercent*100);
            model.addAttribute("userList",list);
            return "admin/index";
=======
    public String index(){
        return "admin/index";
    }
    @PostMapping("/login")
    public String login(@RequestParam String email,@RequestParam String password, HttpSession session, RedirectAttributes attributes) {
        Admin admin = userService.checkUser(email, password);
        System.out.println(admin);;
        if (admin != null) {
            session.setAttribute("admin", admin);
            return "/admin/index";
>>>>>>> 9a66e53171eff92bf88f5bdf58a30715596c289f
        }
        else {
            attributes.addFlashAttribute("message","用户名或密码错误");
            return "redirect:/admin";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("admin");
        return "redirect:/admin";
    }
<<<<<<< HEAD
=======

>>>>>>> 9a66e53171eff92bf88f5bdf58a30715596c289f
}
